package com.wjw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjw.pojo.Book;
import com.wjw.service.BookSersive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("stock")
public class  StockController {
    @Autowired
    private BookSersive bookService;

    @RequestMapping("listStock")
    public String list(@RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
                       @RequestParam(required = false,defaultValue = "10",value = "pageSize") Integer pageSize, Model model, Book book) {
        if (pageNum <= 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageSize <= 0 || pageSize.equals("") || pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Book> qw=new QueryWrapper<>();
        if (book.getBname()!=null){
            qw.like("bname",book.getBname());
        }
        if (book.getType()!=null){
            qw.like("type",book.getType());
        }

        List<Book> list = bookService.list(qw);
        PageInfo<Book> objectPageInfo = new PageInfo<Book>(list);
        model.addAttribute("pageInfo", objectPageInfo);
        return "stockList";
    }

    //调整库存
    @PostMapping("updateStock")
    @ResponseBody
    public String updateStock(Integer bookId,Integer stock){
        Book book = new Book();
        book.setId(bookId);
        book.setStock(stock);
        boolean b = bookService.updateById(book);
        if (b){
            return "OK";
        }else {
            return "error";
        }

    }
}
